package com.apartment.control;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import com.apartment.data.entity.Collect;
import com.apartment.data.entity.User;
import com.apartment.data.view.Collectview;
import com.apartment.sevice.CollectviewService;
import com.apartment.sevice.ServeService;
import com.apartment.sevice.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;

import java.util.List;

/**
 * Author : xuzhiyong
 * Date : 2022/5/27 22:53
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private CollectviewService collectviewService;

    private User user;

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "user/userAdd";
    }

    @RequestMapping("/addUser")
    public String add(User user){
        userService.addUser(user);
        return "redirect:/user/toLogin";
    }

    @RequestMapping("/exit")
    public String exit(){
        return "redirect:/user/toLogin";
    }

    @RequestMapping("/toEdit")
    public String toEdit(Model model, Integer id) {
        User user = userService.findUserById(id);
        model.addAttribute("user",user);
        return "user/userEdit";
    }

    @RequestMapping("/editUser")
    public String edit(Model model,User user){
        userService.updateUser(user);
        model.addAttribute("user",user);
        return "/user/userDetail";
    }

    @RequestMapping("/delete")
    public String delete(int id) {
        userService.deleteUserById(id);
        return "redirect:/user/userList";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "user/userLogin";
    }

    @RequestMapping("/login")
    public String login(@RequestParam("username") String username,@RequestParam("password") String password,Model model){
        User user = userService.findUserByUsername(username);
        model.addAttribute("user",user);
        if (userService.userLogin(username,password)!= null)
            return "user/userIndex";
        else
            return "user/userLoginError";
    }

    @RequestMapping("/userHomePage")
    public String userHome(String username,Model model){
        User user = userService.findUserByUsername(username);
        model.addAttribute("user",user);
        return "user/userHomePage";
    }

    @RequestMapping("/userList")
    public String userList(Model model) {
        System.out.println("查询所有user");
        List<User> users = userService.findAll();
        model.addAttribute("users", users);
        return "user/userList";
    }

    @RequestMapping("/userDetail")
    public String userDetail(Model model,String username){
        User user = userService.findUserByUsername(username);
        model.addAttribute("user",user);
        return "user/userDetail";
    }

    @RequestMapping("/return")
    public String re(){
        return "redirect:/user/userIndex";
    }

    //查看收藏
    @RequestMapping("/collect")
    public String collect(Model model,Integer id){
        List<Collectview> collectviews = collectviewService.findCollectByUserId(id);
        model.addAttribute("collectviews",collectviews);
        return "user/userCollect";
    }

    @RequestMapping("/toForgetPassword")
    public String toforget(){
        return "user/userForgetPassword";
    }

    private static Logger LOG= LoggerFactory.getLogger(UserController.class);

    @Value("${spring.mail.username}")
    private String Mail;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private TemplateEngine templateEngine;

    @PostMapping("/forgetPassword")
    @ResponseBody
    public String sendMail(@RequestParam("mail") String mail){
        User user = userService.findUserByMail(mail);
        if(user !=null){
            String code = user.getPassword();
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(Mail);
            message.setTo(mail);
            message.setSubject("你的密码");
            message.setText("密码为："+code);
            mailSender.send(message);
            System.out.println(message);
            LOG.info("发送成功！！！");
            return code;
        }else {
            String code = "";
            return code;
        }
    }

}
